package org.dictionary;

import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author siarhei
 */
public class DictionaryProvider extends ContentProvider {

    public static final String AUTHORITY = "org.dictionary.DictionaryProvider";

    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);

    public static final String QUIZ_STATS_PATH = "quiz_stats";
    public static final String QUIZ_PATH = "quiz";

    private static final UriMatcher uriMatcher;

    private static final int SEARCH_SUGGEST_QUERY = 1;
    private static final int QUIZ_STATS = 2;
    private static final int QUIZ = 3;
    
    @Override
    public boolean onCreate() {
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Log.d(getClass().getSimpleName(), uri.toString());
        
        switch (uriMatcher.match(uri)) {
            case SEARCH_SUGGEST_QUERY:
                Map<String, String> projectionMap = new HashMap<String, String>();
                projectionMap.put(DictionaryEntry._ID, DictionaryEntry._ID);
                projectionMap.put(DictionaryEntry.ORIGINAL_TEXT, DictionaryEntry.ORIGINAL_TEXT);
                projectionMap.put(SearchManager.SUGGEST_COLUMN_TEXT_1, DictionaryEntry.ORIGINAL_TEXT + " AS " + SearchManager.SUGGEST_COLUMN_TEXT_1);
                projectionMap.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID, DictionaryEntry._ID + " AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID);
                return DictionaryDao.getInstance(getContext()).getCursor(selectionArgs[0], projectionMap);

            case QUIZ_STATS:
                return DictionaryDao.getInstance(getContext()).getStatistics();
            case QUIZ:                
                return DictionaryDao.getInstance(getContext()).getQuizEntries();
                
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
    }

    @Override
    public String getType(Uri arg0) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Uri insert(Uri uri, ContentValues initialValues) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int delete(Uri uri, String where, String[] whereArgs) {
        throw  new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
        Log.d(getClass().getSimpleName(), uri.toString());
        DictionaryEntry entry = DictionaryDao.getInstance(getContext()).get(values.getAsLong(DictionaryEntry._ID));
        entry.setBatch(values.getAsInteger(DictionaryEntry.BATCH));
        entry.setLastUpdate(new Date());
        DictionaryDao.getInstance(getContext()).save(entry);
        return 1;
    }

    static {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST_QUERY);
        uriMatcher.addURI(AUTHORITY, QUIZ_STATS_PATH, QUIZ_STATS);
        uriMatcher.addURI(AUTHORITY, QUIZ_PATH, QUIZ);
    }
}
